package demo02_lock;

/**
 * @author kunkun
 * @className TicketSolution_basic
 * @Description 解决买票问题
 * @date 2025/3/5 16:11
 */
public class TicketSolution_basic {

    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        new Thread(()->{while (ticket.ticketsNum>0) ticket.sail();},"A").start();
        new Thread(()->{while (ticket.ticketsNum>0) ticket.sail();},"B").start();
        new Thread(()->{while (ticket.ticketsNum>0) ticket.sail();},"C").start();
    }


    static class Ticket {

        private int ticketsNum = 20;

        public synchronized void sail() {
            if (ticketsNum > 0){
                System.out.println(Thread.currentThread().getName()+"购买第" + ticketsNum + "张票，剩余" + (ticketsNum--) + "张票");
            }
        }
    }

}

